/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package com.esm.sm800.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;

import com.esm.sm800.R;
import com.zxing.activity.CaptureActivity;

import zuo.biao.library.interfaces.ActivityPresenter;
import zuo.biao.library.util.CommonUtil;
import zuo.biao.library.util.Log;

/**
 * 扫描二维码Activity
 *
 * @author Lemon
 * @use toActivity(ScanActivity.createIntent ( ...));
 */
public class ScanActivity extends CaptureActivity implements ActivityPresenter, OnClickListener {
    public static final String TAG = "ScanActivity";

    public static Intent createIntent(Context context) {
        return new Intent(context, ScanActivity.class);
    }

    @Override
    public Activity getActivity() {
        return this; //必须return this;
    }

    SurfaceView surfaceView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scan_activity);

        surfaceView = findViewById(R.id.svCameraScan);

// 确保SurfaceView准备就绪
        surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                // Surface创建后，可以尝试截屏
                takeScreenshot(holder);
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                // Surface尺寸改变时，也可以更新截图
                takeScreenshot(holder);
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                // Surface销毁时清理相关资源
            }
        });

        init(this, findViewById(R.id.svCameraScan), findViewById(R.id.vfvCameraScan));
    }

    @Override
    public void initView() {//必须调用

    }

    @Override
    public void initData() {
    }

    @Override
    public void initEvent() {
    }

    @Override
    public void onReturnClick(View v) {
        //截取图片

//        finish();
    }

    private void takeScreenshot(SurfaceHolder holder) {
        try {
            // 锁定Surface，获取Canvas
            Canvas canvas = holder.lockCanvas();

            if (canvas != null) {
                // 创建目标Bitmap，尺寸与SurfaceView相同
                Bitmap bitmap = Bitmap.createBitmap(surfaceView.getWidth(), surfaceView.getHeight(), Bitmap.Config.ARGB_8888);

                // 创建临时Canvas，将Surface内容绘制到Bitmap
                Canvas tempCanvas = new Canvas(bitmap);
                canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); // 清除原有内容（可选，根据实际情况）
                surfaceView.draw(tempCanvas); // 将SurfaceView的内容绘制到临时Canvas

                // 保存Bitmap到文件或其他操作
//                saveBitmapToFile(bitmap, "surfaceview_screenshot.png");

                // 释放Canvas
                holder.unlockCanvasAndPost(canvas);
            }
        } catch (Exception e) {

        }
    }

    @Override
    public void onForwardClick(View v) {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.ivCameraScanLight:
//                switchLight(!isOn());
//                break;
            default:
                break;
        }
    }

    @Override
    public boolean isAlive() {
        return false;
    }

    @Override
    public boolean isRunning() {
        return false;
    }

}